<?php $db = new Mypdo(); ?><?php

if (!empty($_SESSION['login'])&&!empty($_SESSION['email'])){

if(!empty($_GET['supprid'])&&!empty($_GET['id'])){


    if($_GET['supprid'] == $_GET['id']){
        $persManager = new PersonneManager($db);
        $locataireManager = new LocataireManager($db);
        $photoManager = new PhotoManager($db);
        $chargeManager = new ChargeManager($db);
        $locationManager = new LocationManager($db);
        $loyerManager = new LoyerManager($db);



        $loyerManager->delLoyerByIdLoc($_GET['supprid']);
        $persManager->delPersByIdLoc($_GET['supprid']);
        $locataireManager->delLocaByIdLoc($_GET['supprid']);
        $chargeManager->delChargeByIdLoc($_GET['supprid']);
        $photoManager->delPhotoByIdLoc($_GET['supprid']);
        $locationManager->delLocation($_GET['supprid']);
        header('Location: index.php?page=107');
    }
}else if(!empty($_GET['id'])&&!empty($_GET['idp'])){

    $photoManager = new PhotoManager($db);
    $photoManager->delPhoto($_GET['id'],$_GET['idp']);

    if($_GET['idp'] == 1){
        $photoManager->updatePremierePhoto($_GET['id']);
    }

    header('Location: index.php?page=113&id='. $_GET['id']);


}else{
    if(empty($_POST)&&!empty($_GET['id'])){
        $immeuManager = new ImmeubleManager($db);
        $locManager = new LocationManager($db);
        $loc = $locManager->getLoc($_GET['id']);

        $listeImmeu = $immeuManager->getList();

        ?>

        <h1>Détail d'une location</h1>

        <form method="post" action="#">

            <p>
            <h3>Information</h3>
            <label for="type_loc">Type de location :</label><select name="type_loc" id="type_loc" required="required">
                <option></option>
                <option value="appart"  <?php echo $loc->getTypeLoc() == 'appart'?'selected':''; ?>>Appartement</option>
                <option value="mais" <?php echo $loc->getTypeLoc() == 'mais'?'selected':''; ?>>Maison</option>
                <option value="chamb" <?php echo $loc->getTypeLoc() == 'chamb'?'selected':''; ?>>Chambre</option>

            </select><br/>


            <label for="id_immeu">Habitation :</label><select name="id_immeu" id="id_immeu" required="required">
                <option></option>
                <?php foreach($listeImmeu as $key=>$value){
                    ?><option value="<?php echo $value->getIdImmeu(); ?>"<?php echo $loc->getIdImmeu() == $value->getIdImmeu()?'selected':''; ?>><?php echo $value->getNomImmeu(); ?></option><?php echo "\n";
                } ?>


            </select>
            <label for="nom_loc">Nom Location :</label><input name="nom_loc" type="text" id="nom_loc" required="required" value="<?php echo $loc->getNomLoc(); ?>" /><br />
            <label for="montant_loc">Montant loyer :</label><input type="number" name="montant_loc" id="montant_loc" step="any" min="0" required="required" value="<?php echo $loc->getMontantLoc(); ?>" /><br />
            <label for="tva_loc">TVA loyer :</label><input type="number" name="tva_loc" id="tva_loc" step="any" min="0" required="required" value="<?php echo $loc->getTvaLoc(); ?>" /><br />
            <label for="descp_loc">Description :</label><textarea rows="4" cols="30" name="descp_loc" id="descp_loc" maxlength="255"  ><?php echo $loc->getDescpLoc(); ?></textarea>

            <label for="surface_loc">Surface :</label><input type="number" name="surface_loc" id="surface_loc" min="0" step="any" required="required" value="<?php echo $loc->getSurfaceLoc(); ?>" /><br />
            <label for="nb_piece_loc">Nombre de pièce :</label><input type="number" name="nb_piece_loc" id="nb_piece_loc" min="1" required="required" value="<?php echo $loc->getNbPieceLoc(); ?>" /><br />
            <label for="nb_chambre_loc">Nombre de chambre :</label><input type="number" name="nb_chambre_loc" id="nb_chambre_loc" min="0" required="required" value="<?php echo $loc->getNbChambreLoc(); ?>" /><br />
            <label for="meuble_loc">Meublé :</label><input type="checkbox" name="meuble_loc" id="meuble_loc" value="1"  <?php echo $loc->getMeubleLoc() == '1'?'checked':''; ?>  /><br />
            <label for="exposition_loc">Exposition :</label><select name="exposition_loc" id="exposition_loc" required="required">
                <option></option>
                <option value="n" <?php echo $loc->getExpositionLoc() == 'n'?'selected':''; ?> >Nord</option>
                <option value="e" <?php echo $loc->getExpositionLoc() == 'e'?'selected':''; ?> >Est</option>
                <option value="o" <?php echo $loc->getExpositionLoc() == 'o'?'selected':''; ?> >Ouest</option>
                <option value="s" <?php echo $loc->getExpositionLoc() == 's'?'selected':''; ?> >Sud</option>
            </select>

            </p>
            <p><input type="reset" value="Annuler" /><input type="submit" value="Modifier" />
                <input id="suppr" name="suppr" type="button" onclick="document.location.href='index.php?page=113&id=<?php echo $_GET['id']; ?>&supprid=<?php echo $loc->getIdLoc(); ?>'" value="Supprimer" />
                <input id="addphoto" name="addphoto" type="button" onclick="document.location.href='index.php?page=115&id=<?php echo $_GET['id']; ?>'" value="Ajouter photo" />
            </p>        </form>

    <?php $photoManager = new PhotoManager($db);
        $listePhoto = $photoManager->getListByIdLoc($loc->getIdLoc());
        if(!empty($listePhoto)){ ?>

        <div class="galerie">
            <table class="tableau">
                <tr>
                    <th>Photo</th>
                    <th>Supprimer</th>
                </tr>
            <?php  foreach ($listePhoto as $value) {

            ?>
            <tr>
                <td>
                    <a class="lightbox" href="image/upload/<?php echo $value->getLienPhoto(); ?>" title="<?php echo $value->getDescpPhoto(); ?>" data-lightbox-gallery="gallery1">
                        <img src="image/upload/<?php echo $value->getLienPhoto(); ?>" alt=""  />
                    </a>
                </td>
                <td>
                    <a  href="index.php?page=113&id=<?php echo $_GET['id'];  ?>&idp=<?php echo $value->getIdPhoto(); ?>">
                        <img id="ico" src="image/erreur.png" alt="Supprimer">
                    </a>
                </td>
            </tr>
    <?php }  ?>
        </table>
        </div>


    <?php
        }

    $locaManager = new LocataireManager($db);
    $loca = $locaManager->getListByIdLoc($_GET['id']);

    ?>
<h1>Liste des locataires</h1>
<table class="tableau" >
    <tr>
        <th>Nom</th>
        <th>Prenom</th>
        <th>Location</th>
        <th>Détail</th>
    </tr>
    <?php foreach ($loca as $valeur) {
        $loc = $locaManager->getLocation($valeur->getIdLoca());
        $pers = $locaManager->getPersonne($valeur->getIdLoca());

//var_dump($pers);
        ?>

        <tr>
            <td><?php
                if(gettype($pers) == 'object'){
                    echo $pers->getNomPers();
                }else{
                    foreach($pers as $val){
                        echo $val->getNomPers().'<br/>';
                    }
                }

                ?></td>
            <td><?php
                if(gettype($pers) == 'object'){
                    echo $pers->getPrenomPers();
                }else{
                    foreach($pers as $val){
                        echo $val->getPrenomPers().'<br/>';
                    }
                }?></td>
            <td><?php
                if(gettype($loc) == 'object'){
                    echo $loc->getNomLoc();
                }else{
                    foreach($loc as $val){
                        echo $val->getNomLoc().'<br/>';
                    }
                }
                ?></td>
            <td><input id="detail" name="detail" type="button" onclick="document.location.href='index.php?page=114&id=<?php echo $valeur->getIdLoca(); ?>'" /></td>
        </tr>

    <?php
    } ?>
</table>

        <input value="Ajouté une locataire" id="add" name="add" type="button" onclick="document.location.href='index.php?page=104&idl=<?php echo $_GET['id']; ?>'" />

<?php $chargeManager = new ChargeManager($db);
      $charge = $chargeManager->getListByIdLoc($_GET['id']);
        //var_dump($charge);
      ?>

<h1>Liste des charge</h1>
        <table class="tableau" >
            <tr>
                <th>Nom</th>
                <th>Montant</th>
                <th>Tva</th>
                <th>Modifier</th>
            </tr>
            <?php
            foreach ($charge as $value) {
                ?>
            <tr>
                <td><?php echo $value->getNomCharge(); ?></td>
                <td><?php echo $value->getMontantCharge(); ?></td>
                <td><?php echo $value->getTvaCharge(); ?></td>
                <td><input id="detail" name="detail" type="button" onclick="document.location.href='index.php?page=118&id=<?php echo $value->getIdCharge(); ?>'" /></td>
            </tr>
            <?php
            }

            ?>
        </table>
    <input value="Ajouté une charge" id="addc" name="addc" type="button" onclick="document.location.href='index.php?page=110&idl=<?php echo $_GET['id']; ?>'" />

    <?php
        }else{
        $locManager = new LocationManager($db);
        $locN = new Location($_POST);
        $locN->setIdLoc($_GET['id']);

        $loc = $locManager->getLoc($_GET['id']);

        $locManager->update($locN);
        header('Location: #');
    }
}
}else{
    header('Location: index.php');
}